package com.jy.adscm.operation.cg.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.CgPurchasePlan;
import com.jy.adscm.operation.cg.bean.CreateCgPlan;
import com.jy.adscm.operation.cg.bean.QueryCgPuchasePlan;
import com.jy.adscm.operation.cg.service.CgPurchasePlanService;
import com.jy.adscm.operation.purchase.bean.CreatePlan;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购计划 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@RestController
@RequestMapping("adscm/operation/cg/cgPurchasePlan")
public class CgPurchasePlanController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    CgPurchasePlanService cgPurchasePlanService;

    /**
     * 增加bom信息
     * @param createCgPlan
     * @param request
     * @return
     */
    @AdscmLogger("增加采购计划信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody CreateCgPlan createCgPlan, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
         Boolean result=  cgPurchasePlanService.createPlan(createCgPlan,token);
        return  new SimpleCoreResponse("200","新增成功",result);
    }

    /**
     * 修改bom信息
     * @param cgPurchasePlan
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @AdscmLogger("修改采购计划信息")
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody CgPurchasePlan cgPurchasePlan, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        cgPurchasePlan.setCreator(token.getUser().getId().toString());
        cgPurchasePlan.setCreateTime(new Date());
        boolean result = cgPurchasePlanService.updateCgPurchasePlan(cgPurchasePlan);
        return  new SimpleCoreResponse("200","修改成功",result);
    }


    /**
     * 根据图纸id查询bom信息
     * @param queryCgPuchasePlan
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("select")
    public SimpleCoreResponse selectCgPurchasePlan(@RequestBody QueryCgPuchasePlan  queryCgPuchasePlan , HttpServletRequest request) throws AdscmSystemException {
        Page<CgPurchasePlan> page =new Page<>(queryCgPuchasePlan.getCurrentPage(),queryCgPuchasePlan.getPageSize());
        page= cgPurchasePlanService.selectCgPurchasePlan(page,queryCgPuchasePlan);
        return  new SimpleCoreResponse("200","查询成功",page);
    }

    @AdscmLogger("审核单条采购计划信息")
    @RequestMapping("approve/{id}")
    public SimpleCoreResponse approve(@PathVariable Long id, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result=  cgPurchasePlanService.approve(id);
        return  new SimpleCoreResponse("200","新增成功",result);
    }
    @AdscmLogger("审核单条采购计划信息")
    @RequestMapping("approves")
    public SimpleCoreResponse approve( @RequestBody  List<Long> ids, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result=  cgPurchasePlanService.approves(ids);
        return  new SimpleCoreResponse("200","新增成功",result);
    }



}

